ConfigMaps 允許你將配置數據與應用程序代碼分離。
創建 ConfigMap:
kubectl create configmap my-config --from-literal=APP_COLOR=blue --from-literal=APP_MODE=prod
或者,創建一個 YAML 文件 my-config.yaml
:
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
data:
APP_COLOR: blue
APP_MODE: prod
然後應用:
kubectl apply -f my-config.yaml
使用 ConfigMap 在 Pod 中:
創建 config-pod.yaml
:
apiVersion: v1
kind: Pod
metadata:
name: config-pod
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "env" ]
envFrom:
- configMapRef:
name: my-config
應用並查看結果:
kubectl apply -f config-pod.yaml
kubectl logs config-pod
Secrets 用於存儲和管理敏感信息,如密碼、OAuth 令牌和 SSH 密鑰。
創建 Secret:
kubectl create secret generic my-secret --from-literal=username=admin --from-literal=password=t0p-Secret
或者,創建一個 YAML 文件 my-secret.yaml
(注意:值需要是 base64 編碼的):
apiVersion: v1
kind: Secret
metadata:
name: my-secret
type: Opaque
data:
username: YWRtaW4= # base64 encoded 'admin'
password: dDBwLVNlY3JldA== # base64 encoded 't0p-Secret'
然後應用:
kubectl apply -f my-secret.yaml
在 Pod 中使用 Secret:
創建 secret-pod.yaml
:
apiVersion: v1
kind: Pod
metadata:
name: secret-pod
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "env" ]
envFrom:
- secretRef:
name: my-secret
應用並查看結果:
kubectl apply -f secret-pod.yaml
kubectl logs secret-pod
環境變量可以直接在 Pod 定義中設置,也可以從 ConfigMaps 和 Secrets 中獲取。
創建使用環境變量的 Pod:
創建 env-pod.yaml
:
apiVersion: v1
kind: Pod
metadata:
name: env-pod
spec:
containers:
- name: env-container
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "env" ]
env:
- name: DIRECT_VAR
value: "directly set value"
- name: CONFIG_VAR
valueFrom:
configMapKeyRef:
name: my-config
key: APP_COLOR
- name: SECRET_VAR
valueFrom:
secretKeyRef:
name: my-secret
key: username
應用並查看結果:
kubectl apply -f env-pod.yaml
kubectl logs env-pod
PV 和 PVC 提供了一種抽象層,使得存儲資源的管理與使用分離。
創建持久卷(PV):
創建 my-pv.yaml
:
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: manual
hostPath:
path: "/mnt/data"
應用:
kubectl apply -f my-pv.yaml
創建持久卷聲明(PVC):
創建 my-pvc.yaml
:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 500Mi
storageClassName: manual
應用:
kubectl apply -f my-pvc.yaml
使用 PVC 在 Pod 中:
創建 pv-pod.yaml
:
apiVersion: v1
kind: Pod
metadata:
name: pv-pod
spec:
containers:
- name: task-pv-container
image: nginx
volumeMounts:
- mountPath: "/usr/share/nginx/html"
name: task-pv-storage
volumes:
- name: task-pv-storage
persistentVolumeClaim:
claimName: my-pvc
應用:
kubectl apply -f pv-pod.yaml
我們將部署一個 MySQL 數據庫作為示例。
創建 Secret 用於 MySQL 密碼:
kubectl create secret generic mysql-secret --from-literal=mysql-root-password=rootpassword
創建持久卷和持久卷聲明:
創建 mysql-pv-pvc.yaml
:
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
storageClassName: manual
hostPath:
path: "/mnt/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: manual
應用:
kubectl apply -f mysql-pv-pvc.yaml
部署 MySQL:
創建 mysql-deployment.yaml
:
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
spec:
selector:
matchLabels:
app: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql:5.7
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-secret
key: mysql-root-password
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-storage
persistentVolumeClaim:
claimName: mysql-pvc
應用:
kubectl apply -f mysql-deployment.yaml
創建 MySQL 服務:
創建 mysql-service.yaml
:
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
ports:
- port: 3306
selector:
app: mysql
clusterIP: None
應用:
kubectl apply -f mysql-service.yaml
驗證部署:
kubectl get pods
kubectl get pv
kubectl get pvc
kubectl get services
這個實作過程涵蓋了 Kubernetes 的配置管理、機密管理、環境變量使用、持久存儲和有狀態應用部署。通過這些步驟,你可以學習如何在實際環境中應用這些概念。